#include "Reader.h"
#include "Tokenizer.h"
#include "Scuba.h"
#include "Datalyzer.h"

#include "ArgumentParser.h"
#include "Helper.h"

#include <string>
#include <iostream>
#include <conio.h>

std::string gsFileName = "./test.c";

Reader xReader;
Tokenizer xTokenizer;
Scuba xScubaDiver;
Datalyzer xDatalyzer;

CArgumentParser* gpxArgumentParser;

using namespace std;

MinesweeperOptions gxMinesweeperOptions;

int parseCommandLineMinesweeper(int argc, char**argv)
{
	gpxArgumentParser=new CArgumentParser("vfpth");

	gpxArgumentParser->m_Helper->fillProgramName("nMinesweeper - Data scout");
	gpxArgumentParser->m_Helper->fillHeadMessage("(C)2009 sonofdelphi\nDisplays information regarding data  defined in file.");
	gpxArgumentParser->m_Helper->fillUsageLine("nMinesweeper [OPTIONS] <SourceFile>\nPREVIEW VERSION (debug demo only)");
	gpxArgumentParser->fillHelpForOption('v',"Variables");
	gpxArgumentParser->fillHelpForOption('f',"Fields");
	gpxArgumentParser->fillHelpForOption('p',"Function prototypes");
	gpxArgumentParser->fillHelpForOption('t',"Data Types");
	gpxArgumentParser->fillHelpForOption('h',"Displays this help message");
	gpxArgumentParser->m_Helper->fillTailMessage("Neptuner Suite Component. Build:" __DATE__"/"__TIME__  "\nThis program comes with absolutely no warranty.\n");

	int nParseEndPos = gpxArgumentParser->parse(argc,argv);
	
	if(!gpxArgumentParser->wasParseSuccessful())
	{
		gpxArgumentParser->m_Helper->displayHelp();
		delete gpxArgumentParser;
		cerr<<"Invalid option supplied"<<endl;
		return -1;
	}
	
	//gxMinesweeperOptions.m_bBareSummary = gpxArgumentParser->wasOptionSupplied('b');
	//gxMinesweeperOptions.m_bListOnly = gpxArgumentParser->wasOptionSupplied('l');
	//gxMinesweeperOptions.m_bReport = gpxArgumentParser->wasOptionSupplied('x');
	//gxMinesweeperOptions.m_bModeQuiet = gpxArgumentParser->wasOptionSupplied('q');

	return nParseEndPos+1;
}

int main(int argc, char**argv)
{
	int nProgArgIndex = parseCommandLineMinesweeper(argc, argv);
	if(nProgArgIndex<0)
	{
		return 1;
	}

	if(gpxArgumentParser->wasOptionSupplied('h'))
	{
		gpxArgumentParser->m_Helper->displayHelp();
		delete gpxArgumentParser;
		return 0;
	}

	if(nProgArgIndex<argc)
	{
			gsFileName=argv[nProgArgIndex];
	}
	else
	{
		cerr<<"Attempting to proceed with default test file: "<<gsFileName<<endl;
	}

//    if(!gxMinesweeperOptions.m_bModeQuiet)
		cerr<<"\n<<"<<gsFileName<<">>"<< " data INFORMATION\n\n";
    xReader.gobble(gsFileName);

    xTokenizer.tokenize(xReader.getStream());

    xScubaDiver.setObserver(&xDatalyzer);

    int nListSize;
    nListSize = (int)xTokenizer.m_asTokens.size();
    for(int nLooper = 0; nLooper<nListSize; nLooper++)
    {
        xScubaDiver.feed(xTokenizer.m_asTokens[nLooper]);
    }

    nListSize = (int)xScubaDiver.m_pxCurrentScope->m_apxChildren.size();

    xDatalyzer.dump(&gxMinesweeperOptions);

//    if(!gxMinesweeperOptions.m_bModeQuiet)
	    cerr<<"\n<<"<<gsFileName<<">>"<< " END OF data INFORMATION\n";

	if(argc==1)
	{
		cout<<"Press any key to continue..";
		_getch();
	}

    return 0;
}


